package com.ctgu.springstart.controller;

import com.ctgu.springstart.domain.Article;
import com.ctgu.springstart.domain.JsonData;
import com.ctgu.springstart.repository.ArticleRepository;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/es")
public class ArticleController {

    @Autowired
    private ArticleRepository articleRepository;

    @GetMapping("save")
    public Object save(){
        Article article = Article.builder()
                .id(2L)
                .pv(888)
                .content("this is content")
                .title("I love xdclass 教程")
                .summary("概要搜索")
                .build();
        articleRepository.save(article);
        return JsonData.builder().data(article).build();
    }

    @GetMapping("search")
    public Object search(String title){
        QueryBuilder queryBuilder = QueryBuilders.matchQuery("title", title);
        Iterable<Article> list = articleRepository.search(queryBuilder);
        return JsonData.builder().data(list).build();
    }

}
